$(function () {
    $("#jqGrid").jqGrid({
        url: '../message/list/new',
        datatype: "json",
        colModel: [			
//			{ label: 'id', name: 'id', width: 50, key: true },
			{ label: '消息类型', name: 'messageType', width: 80 }, 			
//			{ label: '消息标签', name: 'messageTag', width: 80 }, 			
			{ label: '消息标题', name: 'messageHeader', width: 80 }, 			
			{ label: '消息简介', name: 'messageProfile', width: 80 }, 			
//			{ label: '消息内容', name: 'messageContent', width: 80 }, 			
//			{ label: '消息封面url', name: 'messageCoverUrl', width: 80 }, 			
//			{ label: '展现方式', name: 'displayMode', width: 80 }, 			
//			{ label: '消息链接', name: 'messageLink', width: 80 }, 			
//			{ label: '接收消息对象类型', name: 'receiveMessageObjectType', width: 80 }, 			
//			{ label: '接收消息对象', name: 'receiveMessageObjects', width: 80 }, 			
//			{ label: '是否启用第三方推送', name: 'ifPush', width: 80 }, 			
//			{ label: '推送方式', name: 'whetherPush', width: 80 }, 			
			{ label: '推送时间', name: 'pushTime', width: 80 }, 			
//			{ label: '渠道标识', name: 'channelid', width: 80 }, 			
			{ label: '状态', name: 'status', width: 80 }, 			
//			{ label: '消息来源', name: 'messageSource', width: 80 }, 			
			{ label: '展示时间', name: 'displayTime', width: 80 }, 			
			{ label: '过期时间', name: 'timeExpired', width: 80 },			
//			{ label: '其它1', name: 'other1', width: 80 }, 			
//			{ label: '其它2', name: 'other2', width: 80 }, 			
//			{ label: '其它3', name: 'other3', width: 80 }, 			
//			{ label: '其它4', name: 'other4', width: 80 }, 			
//			{ label: '其它5', name: 'other5', width: 80 }, 			
//			{ label: '其它6', name: 'other6', width: 80 }, 			
//			{ label: '其它7', name: 'other7', width: 80 }, 			
//			{ label: '其它8', name: 'other8', width: 80 }, 			
//			{ label: '其它9', name: 'other9', width: 80 }, 			
//			{ label: '其它10', name: 'other10', width: 80 }, 			
			{ label: '发布时间', name: 'publishtime', width: 80 }			
//			{ label: '发布人', name: 'publishuser', width: 80 }, 			
//			{ label: '创建时间', name: 'createtime', width: 80 }, 			
//			{ label: '创建人', name: 'createuser', width: 80 }, 			
//			{ label: '修改时间', name: 'updatetime', width: 80 }, 			
//			{ label: '修改人', name: 'updateuser', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{ //消息类型
			messageType: null,
			//消息标题
			messageHeader: null,
			//消息状态
			status: null,
			//消息来源
			messageSource:null,
			//展示时间
			displayTime: null,
			//过期时间
			timeExpired: null,
			//查询类型
			qstatus:null
		},
		showList: true,
		title: null,
		message: {},
		typeList: {},
		qMessageSourceStatus:true,
		messageTypeShowStatus:true,
		messageSourceButton:true,
		receiveMessageObjectTypeStatus:true,
		displayModeShowStatus:true,
		ifPushStatus:true,
		whetherPushStatus:true,
		show:false,
		messageStatus:true,
		checkImage1:false,
		checkImage2:false,
		checkImage3:false,
		checkImage4:false,
		checkImage5:false,
		checkImage6:false,
		checkImage7:false,
		checkImage8:false,
		checkImage9:false,
		checkImage10:false,
		checkImage11:false,
		checkImage12:false,
		checkImage13:false,
		checkImage14:false,
		checkImage15:false,
		checkImage16:false,
		pendingAuditButton:false
    },  
	//创建后加载
	created: function() {
		typeList = {};
		//获取下拉框
		this.getTypeList();
	},
	methods: {
		//新建查询
		query: function () {
			vm.q.qstatus="new";
			vm.reload();
		},
		//提交审核查询
		queryPendingAudit: function () {
			vm.q.qstatus="pendingAudit";
			vm.reload();
		},
		//审核查询
		queryAuditThrough: function () {
			vm.q.qstatus="auditThrough";
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.message = {};
			typeList = {};
			//获取下拉框
			vm.getTypeList();
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id);
            //获取下拉框
			vm.getTypeList();
		},
		saveOrUpdate: function (event) {
			
			if(this.check()){
							
					alert("信息没有填写完整！");
			}else{
				
				// 推送时间
				if(vm.message.pushTime){
					vm.message.pushTime=new Date(vm.message.pushTime);
				}
				//展示时间
				if(vm.message.displayTime){
					vm.message.displayTime=new Date(vm.message.displayTime);
				}
				//过期时间
				if(vm.message.timeExpired){
					vm.message.timeExpired=new Date(vm.message.timeExpired);
				}
				var url = vm.message.id == null ? "../message/save" : "../message/update";
				$.ajax({
					type: "POST",
				    url: url,
				    data: JSON.stringify(vm.message),
				    success: function(r){
				    	if(r.code === 0){
							alert('操作成功', function(index){
//								vm.reload();
								vm.query();
							});
						}else{
							alert(r.msg);
						}
					}
				});
				
			}
			
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../message/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		showMessageList: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "查看";

            vm.getInfo(id);
            //获取下拉框
//			this.getTypeList();
//			this.messageSourceButtonShow();
//			this.messageStatusShow();
		},
		getInfo: function(id){
			$.get("../message/info/"+id, function(r){
                vm.message = r.message;
                vm.messageTypeShow();
				vm.receiveMessageObjectType();
				vm.displayModeShow();
				vm.ifPushShow();
				vm.whetherPushShow();
				vm.messageStatusShow();
				vm.messageSourceButtonShow();
				vm.pendingAuditButtonShow();
				vm.getTypeList();
            });
		},
		reload: function (event) {
			vm.showList = true;
			console.info("--------");
			console.info( vm.q);
			console.info( vm.q.displayTime);
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{
					//消息类型
					'messageType': vm.q.messageType,
					//消息标题
					'messageHeader': vm.q.messageHeader,
					//消息状态
					'status': vm.q.status,
					//消息来源
					'messageSource': vm.q.messageSource,
					//展示时间
					'displayTime': vm.q.displayTime,
					//过期时间
					'timeExpired': vm.q.timeExpired,
//					'qstatus':vm.q.qstatus
					'qstatus':"new"
					
				},
                page:page
            }).trigger("reloadGrid");
		},
		getTypeList: function(){
			$.get("../message/select", function(r){
				vm.typeList = r.list;
			});
		},
		//查询列表消息来源
		qMessageSourceShow:function(){
			var str="自建消息";
			var strs=vm.q.messageSource;
			if(strs.indexOf(str) >= 0){
				this.qMessageSourceStatus=true;
			}else{
				this.qMessageSourceStatus=false;
			}
		},
		//查询列表消息来源
		messageSourceButtonShow:function(){
			var str="自建消息";
			var strs=vm.message.messageSource;
			if(strs.indexOf(str) >= 0){
				this.messageSourceButton=true;
			}else{
				this.messageSourceButton=false;
			}
		},
		//不同消息类型按钮数量不同
		messageStatusShow:function(){
			var str="已发布";
			var strs=vm.message.status;
			if(strs.indexOf(str) >= 0){
				this.messageStatus=false;
			}else{
				this.messageStatus=true;
			}
		},
		
		//消息类型
		messageTypeShow:function(){
			var str="滚动播放消息";
			var strs=vm.message.messageType;
			if(strs.indexOf(str) >= 0){
				this.messageTypeShowStatus=false;
			}else{
				this.messageTypeShowStatus=true;
			}
		},
		//接收对象
		receiveMessageObjectType:function(){
			var str="定向";
			var strs=vm.message.receiveMessageObjectType;
			if(strs.indexOf(str) >= 0){
				this.receiveMessageObjectTypeStatus=true;
			}else{
				this.receiveMessageObjectTypeStatus=false;
			}
		},
		//打开方式
		displayModeShow:function(){
			var str="直接打开";
			var strs=vm.message.displayMode;
			if(strs.indexOf(str) >= 0){
				this.displayModeShowStatus=true;
			}else{
				this.displayModeShowStatus=false;
			}
		},
		//第三方推送
		ifPushShow:function(){
			var str=1;
			var strs=vm.message.ifPush;
			if(strs==str){
				this.ifPushStatus=true;
			}else{
				this.ifPushStatus=false;
			}
		},
		//推送方式
		whetherPushShow:function(){
			var str=0;
			var strs=vm.message.whetherPush;
			if(strs==str){
				this.whetherPushStatus=true;
			}else{
				this.whetherPushStatus=false;
			}
		},
		//提交审核按钮
		pendingAuditButtonShow:function(){
				var str="失效";
			var strs=vm.message.status;
			if(strs==str){
				this.pendingAuditButton=true;
			}else{
				this.pendingAuditButton=false;
			}
		},
		check : function() {
			// 消息类型
			var messageType = vm.message.messageType;
			if (messageType == null|| messageType =="") {
				this.checkImage1 = true;
				return true;
			} else {
				this.checkImage1 = false;
			}
			var str="滚动播放消息";
			var strs=vm.message.messageType;
			if(strs.indexOf(str) >= 0){
					// 消息标题
				var messageHeader = vm.message.messageHeader;
				if (messageHeader == null||messageHeader=="") {
					this.checkImage3 = true;
					return true;
				} else {
					this.checkImage3 = false;
				}
				// 接收消息对象类型
				var receiveMessageObjectType = vm.message.receiveMessageObjectType;
				if (receiveMessageObjectType == null) {
					this.checkImage9 = true;
					return true;
				} else {
					this.checkImage9 = false;
				}
				//接收对象
				var str="定向";
				var strs=vm.message.receiveMessageObjectType;
				if(strs.indexOf(str) >= 0){
					// 接收消息对象
					var receiveMessageObjects = vm.message.receiveMessageObjects;
						if (receiveMessageObjects == null|| receiveMessageObjects =="") {
							this.checkImage10 = true;
							return true;
						} else {
							this.checkImage10 = false;
						}
				}
			}else{
				// 消息标签
				var messageTag = vm.message.messageTag;
				if (messageTag == null|| messageTag =="") {
					this.checkImage2 = true;
					return true;
				} else {
					this.checkImage2 = false;
				}
				// 消息标题
				var messageHeader = vm.message.messageHeader;
				if (messageHeader == null|| messageHeader =="") {
					this.checkImage3 = true;
					return true;
				} else {
					this.checkImage3 = false;
				}
				// 消息简介
				var messageProfile = vm.message.messageProfile;
				if (messageProfile == null|| messageProfile =="") {
					this.checkImage4 = true;
					return true;
				} else {
					this.checkImage4 = false;
				}
				// 消息封面url
				var messageCoverUrl = vm.message.messageCoverUrl;
				if (messageCoverUrl == null|| messageCoverUrl =="") {
					this.checkImage5 = true;
					return true;
				} else {
					this.checkImage5 = false;
				}
				// 展现方式
				var displayMode = vm.message.displayMode;
				if (displayMode == null|| displayMode =="") {
					this.checkImage6 = true;
					return true;
				} else {
					this.checkImage6 = false;
				}
				var str="直接打开";
				var strs=vm.message.displayMode;
				if(strs.indexOf(str) >= 0){
					// 消息内容
					var messageContent = vm.message.messageContent;
					if (messageContent == null|| messageContent =="") {
						this.checkImage7 = true;
						return true;
					} else {
						this.checkImage7 = false;
					}
				}else{
					// 消息链接
					var messageLink = vm.message.messageLink;
					if (messageLink == null|| messageLink =="") {
						this.checkImage8 = true;
						return true;
					} else {
						this.checkImage8 = false;
					}
					
				}
				// 接收消息对象类型
				var receiveMessageObjectType = vm.message.receiveMessageObjectType;
				if (receiveMessageObjectType == null|| receiveMessageObjectType =="") {
					this.checkImage9 = true;
					return true;
				} else {
					this.checkImage9 = false;
				}
				//接收对象
				var str="定向";
				var strs=vm.message.receiveMessageObjectType;
				if(strs.indexOf(str) >= 0){
					// 接收消息对象
					var receiveMessageObjects = vm.message.receiveMessageObjects;
						if (receiveMessageObjects == null|| receiveMessageObjects =="") {
							this.checkImage10 = true;
							return true;
						} else {
							this.checkImage10 = false;
						}
				}
				// 是否启用第三方推送
				var ifPush = vm.message.ifPush;
				if (ifPush == null) {
					this.checkImage11 = true;
					return true;
				} else {
					this.checkImage11 = false;
				}
				//第三方推送
				var str=1;
				var strs=vm.message.ifPush;
				if(strs==str){
					// 推送方式
					console.info("推送方式");
					var whetherPush = vm.message.whetherPush;
					if (whetherPush == null) {
						this.checkImage12 = true;
						return true;
					} else {
						this.checkImage12 = false;
					}
					//推送方式
					var str=0;
					var strs=vm.message.whetherPush;
					if(strs==str){
						// 推送时间
						var pushTime = vm.message.pushTime;
						if (pushTime == null|| pushTime =="") {
							this.checkImage13 = true;
							return true;
						} else {
							this.checkImage13 = false;
						}
					}
					
				}
				
			}
			
			//展示时间
			var displayTime = vm.message.displayTime;
			if (displayTime == null|| displayTime =="") {
				this.checkImage14 = true;
				return true;
			} else {
				this.checkImage14 = false;
			}
			//过期时间
			var timeExpired = vm.message.timeExpired;
			if (timeExpired == null|| timeExpired =="") {
				this.checkImage15 = true;
				return true;
			} else {
				this.checkImage15 = false;
			}
				return false;
		},
		//提交审核
		pendingAudit: function (event) {
			confirm('确定这条消息要提交审核？', function(){
				$.ajax({
					type: "POST",
				    url: "../message/pendingAudit",
				    data: JSON.stringify(vm.message),
				    success: function(r){
				    	if(r.code === 0){
							alert('操作成功', function(index){
//								vm.reload();
								vm.queryPendingAudit();

							});
						}else{
							alert(r.msg);
						}
					}
				});
			})
		},
		//审核通过
		auditThrough: function (event) {
			confirm('确定这条消息审核通过？', function(){
				$.ajax({
					type: "POST",
				    url: "../message/auditThrough",
				    data: JSON.stringify(vm.message),
				    success: function(r){
				    	if(r.code === 0){
							alert('操作成功', function(index){
//								vm.reload();
								vm.queryAuditThrough();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			})
		},
		//发布
		alreadyReleased: function (event) {
			confirm('确定要发布这条消息？', function(){
				$.ajax({
					type: "POST",
				    url: "../message/alreadyReleased",
				    data: JSON.stringify(vm.message),
				    success: function(r){
				    	if(r.code === 0){
							alert('操作成功', function(index){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			})
		},
		//失效
		invalid: function (event) {
			confirm('确定这条消息失效？', function(){
				$.ajax({
					type: "POST",
				    url: "../message/invalid",
				    data: JSON.stringify(vm.message),
				    success: function(r){
				    	if(r.code === 0){
							alert('操作成功', function(index){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			})
		}
	}
});